package com.ck.client.service.impl;

import com.ck.client.dao.ProgramDownloadMapper;
import com.ck.common.model.ProgramDownload;
import com.ck.common.model.ProgramDownloadFailureLog;
import com.ck.client.service.ProgramDownloadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName ProgramDownloadServiceImpl
 * @Description 节目下载service实现类
 * @Author HanQinrui
 * @Date 2018/7/19 16:18
 * @Version 1.0
 **/
@Service(value = "programDownloadService")
public class ProgramDownloadServiceImpl implements ProgramDownloadService {
    @Autowired
    private ProgramDownloadMapper programDownloadMapper;
    /**
     * 通过终端MAC和节目版本号获取下载信息
     * @param deviceCode              终端code
     * @param downloadProgramVersion 节目版本号
     * @return
     */
    @Override
    public ProgramDownload getProgramDownloadByCodeAndXcVersion(String deviceCode, String downloadProgramVersion) {
        return programDownloadMapper.getProgramDownloadByCodeAndXcVersion(deviceCode, downloadProgramVersion);
    }

    /**
     * 保存节目下载信息
     * @param programDownload 节目下载信息
     */
    @Override
    public void saveProgramDownload(ProgramDownload programDownload) {
        programDownloadMapper.saveProgramDownload(programDownload);
    }

    /**
     * 更新节目下载信息
     * @param programDownload
     */
    @Override
    public void updateProgramDownload(ProgramDownload programDownload) {
        programDownloadMapper.updateProgramDownload(programDownload);
    }

    /**
     * 记录下载失败日志
     * @param deviceCode              终端code
     * @param downloadProgramVersion 节目版本号
     * @param failureReason          失败原因
     */
    @Override
    public void saveDownloadFailLog(String deviceCode, String downloadProgramVersion, String failureReason) {
        ProgramDownloadFailureLog programDownloadFailureLog =
                new ProgramDownloadFailureLog(deviceCode, downloadProgramVersion, System.currentTimeMillis(), failureReason);
        programDownloadMapper.saveDownloadFailLog(programDownloadFailureLog);
    }
}
